//
//  MainTabBarController.m
//  TaskManager
//
//  Created by Sun on 2019/3/1.
//  Copyright © 2019 Sun. All rights reserved.
//

#import "MainTabBarController.h"
#import "MainNavigationController.h"
#import "OrderVC.h"
#import "CashierVC.h"
#import "MineVC.h"

@interface MainTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MainTabBarController

+ (instancetype)instance{
    UIViewController *vc = [[[UIApplication sharedApplication] delegate]window].rootViewController;
    if ([vc isKindOfClass:[MainTabBarController class]]) {
        return (MainTabBarController *)vc;
    }else{
        return nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Color_backgroundColor;
    
    OrderVC *orderVC = [OrderVC new];
    [self createTabBarWithChildVC:orderVC andItem:@"订单" normalImage:@"tab_order" selectedImage:@"tab_order_sel" itemIndex:0];
    CashierVC *cashierVC = [CashierVC new];
    [self createTabBarWithChildVC:cashierVC andItem:@"收银台" normalImage:@"icon_cashier" selectedImage:@"icon_cashier_sel" itemIndex:0];
    MineVC *mineVC = [MineVC new];
    [self createTabBarWithChildVC:mineVC andItem:@"我的" normalImage:@"tab_my" selectedImage:@"tab_my_sel" itemIndex:0];
    self.delegate = self;
    self.selectedIndex = 0;
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
}

/**
 *  自定义底部tabbar按方法
 *
 *  @param title       tabbar按钮的标题
 *  @param normalImg   未选择时候的图标
 *  @param selectedImg 选中时候的图标
 *  @param index       顺序
 */
-(void)createTabBarWithChildVC:(UIViewController *)vc andItem:(NSString *)title normalImage:(NSString *)normalImg selectedImage:(NSString *)selectedImg itemIndex:(NSInteger)index
{
    MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:vc];
//    NSDictionary *navTitleDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0xffffff),NSForegroundColorAttributeName,[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
//    [vc.navigationController.navigationBar setTitleTextAttributes:navTitleDic];
    UIImage *myImage = [UIImage imageNamed:normalImg];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.image = myImage;
    
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImg];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = mySelectedImage;
//    [vc.tabBarItem setImageInsets:UIEdgeInsetsMake(-2, 2, 2, 0)];
    vc.tabBarItem.title = title;
    NSDictionary *titleSelectedDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0x3D3E5A),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil];
    NSDictionary *titleNormalDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0x9E9FB5),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil];
    [vc.tabBarItem setTitleTextAttributes:titleNormalDic forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:titleSelectedDic forState:UIControlStateSelected];
    [self addChildViewController:nav];
}

- (void)dealloc {
    NSLog(@"---- %@ ---- dealloc ----",self.class);
}

@end
